package com.coolingme.kafka.consumer;

import com.alibaba.fastjson.JSON;
import com.coolingme.dto.KafkaDataDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * kafka消费者，整合spring
 *
 * @author wangyue
 * @date 2021/10/25 11:08
 */
@Slf4j
@Component
@ConditionalOnProperty(value = "system.consumer.enable", havingValue = "true")
public class KafkaConsumer {

    static {
        log.info("------------KafkaConsumer init-------------");
    }

    /**
     * 消费对象
     */
    @KafkaListener(topics = "coolingme")
    public void receiveCoolingme(ConsumerRecord<?, ?> record) {
        KafkaDataDTO kafkaDataDTO = JSON.parseObject(record.value().toString(), KafkaDataDTO.class);
        log.info("消费组为：demo_test_group_1, receive, 主题为 ：{} , offset is {} , value is {}",
                record.topic(), record.offset(), kafkaDataDTO);
    }


    /**
     * groupId设置消费者所属组
     *
     * @param record 获取的记录
     */
    @KafkaListener(topics = "test-topic", groupId = "demo_test_group_1")
    public void receive(ConsumerRecord<?, ?> record) {
        log.info("消费组（同1）为：demo_test_group_1, 主题为 ：{} , offset is {} , value is {}",
                record.topic(), record.offset(), record.value());
    }

    /**
     * 测试同一个消费组内的消费者一起消费时，不会重复消费
     *
     * @param record 获取的记录
     */
    @KafkaListener(topics = "test-topic", groupId = "demo_test_group_1")
    public void receive1(ConsumerRecord<?, ?> record) {
        log.info("消费组（同2）为：demo_test_group_1, 主题为 ：{}, offset is {}, value is {}",
                record.topic(), record.offset(), record.value());
    }

    /**
     * 测试不属于同一个消费组内的消费者一起消费时，可以重复消费
     *
     * @param record 获取的记录
     */
    @KafkaListener(topics = "test-topic", groupId = "demo_test_group_2")
    public void receive2(ConsumerRecord<?, ?> record) {
        log.info("消费组（不同）为：demo_test_group_2, 主题为 ：{}, offset is {}, value is {}",
                record.topic(), record.offset(), record.value());
    }

    /**
     * 不指定具体的消费者组，即为默认的default_consumer_group
     *
     * @param record 获取的记录
     */
    @KafkaListener(topics = "test-topic")
    public void receive3(ConsumerRecord<?, ?> record) {
        log.info("消费组为： default_consumer_group, receive3, 主题为 ：{}, offset is {}, value is {}",
                record.topic(), record.offset(), record.value());
    }


    /*@KafkaListener(topics = "test-topic", groupId = "demo_test_group_id2")
    public void topic_test(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info("topic_test 消费了： Topic:" + topic + ",Message:" + msg);
            ack.acknowledge();
        }
    }*/

}
